package com.lang;

import org.junit.Test;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;

/**
 * 创建线程的方式:
 * 1. 继承Thread类
 * 2. 实现Runnable接口
 * 3. 实现Callable接口
 * 4. 线程池
 * 5. CompletableFuture
 * 6. ThreadGroup
 * 7. FutureTask
 * 8. 匿名内部类或Lambda
 * 9. 定时器 Timer
 * 10. ForkJoinPool
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-31 10:21:32
 */
public class CreateThreadTest {

    @Test
    public void test6() {
        ForkJoinPool pool = new ForkJoinPool();
        pool.execute(() -> {
            System.out.println("hello");
        });

        pool.shutdown();
    }

    @Test
    public void test5() throws InterruptedException {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Hello");
            }
        }, 2000);
        TimeUnit.SECONDS.sleep(3);
    }

    @Test
    public void test4() throws ExecutionException, InterruptedException {
        FutureTask<Integer> task = new FutureTask<>(() -> 1);
        new Thread(task).start();

        Integer integer = task.get();
        System.out.println(integer);
    }

    @Test
    public void test3() {
        ThreadGroup threadGroup = new ThreadGroup("group-1");
        new Thread(threadGroup, () -> {
            System.out.println("t1 " + Thread.currentThread().getName());
        }, "T1").start();
        new Thread(threadGroup, () -> {
            System.out.println("t2 " + Thread.currentThread().getName());
        }, "T2").start();
        new Thread(threadGroup, () -> {
            System.out.println("t3 " + Thread.currentThread().getName());
        }, "T3").start();
    }

    @Test
    public void test2() throws ExecutionException, InterruptedException {
        CompletableFuture<String> async = CompletableFuture.supplyAsync(() -> {
            return "Hello";
        });
        String s = async.get();
        System.out.println(s);
    }

    private static final class ExtendThread extends Thread {

        @Override
        public void run() {
            super.run();
        }
    }

    private static final class ImplRunnable implements Runnable {

        @Override
        public void run() {

        }
    }

    private static final class ImplCallable implements Callable<Object> {

        @Override
        public Object call() throws Exception {
            return null;
        }
    }

    @Test
    public void test1() {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
    }
}
